package com.example.zhaojianxu.signin.common.http

import com.example.englishstudy.EnglishApi
import com.example.englishstudy.constance.Constance.ENGLISH_BASE_URL
import com.example.zhaojianxu.signin.bank.http.BankApi
import com.example.zhaojianxu.signin.car.http.CarApi
import com.example.zhaojianxu.signin.common.constance.ComConstance.COMMON_BASE_URL
import com.example.zhaojianxu.signin.englishgame.http.EnglishGameApi
import com.example.zhaojianxu.signin.psychological.http.PsycApi
import com.example.zhaojianxu.signin.securities.http.SecuritiesApi
import com.example.zhaojianxu.signin.signIn.contsance.Constance.SIGN_BASE_URL
import com.example.zhaojianxu.signin.signIn.http.SignApi
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class HttpUtil private constructor() {

    init {
        retrofitBuilder = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
    }

    companion object {
        val instance: HttpUtil by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { HttpUtil() }
        var retrofit: Retrofit? = null
        var retrofitBuilder: Retrofit.Builder? = null
    }

    fun getCommonApi(): CommonApi? {
        if (retrofit == null) retrofit = retrofitBuilder?.baseUrl(COMMON_BASE_URL)?.build()

        return retrofit?.create(CommonApi::class.java)
    }

    fun getSignApi(): SignApi? {

        if (retrofit == null) retrofit = retrofitBuilder?.baseUrl(SIGN_BASE_URL)?.build()

        return retrofit?.create(SignApi::class.java)
    }

    fun getPsycApi(): PsycApi? {

        if (retrofit == null) retrofit = retrofitBuilder?.baseUrl(COMMON_BASE_URL)?.build()

        return retrofit?.create(PsycApi::class.java)
    }

    fun getEnglishApi(): EnglishApi? {
        if (retrofit == null) retrofit = retrofitBuilder?.baseUrl(COMMON_BASE_URL)?.build()
        return retrofit?.create(EnglishApi::class.java)
    }

    fun getEnglishGameApi(): EnglishGameApi? {
        if (retrofit == null) retrofit = retrofitBuilder?.baseUrl(COMMON_BASE_URL)?.build()

        return retrofit?.create(EnglishGameApi::class.java)
    }

    fun getSecuritiesApi(): SecuritiesApi? {
        if (retrofit == null) retrofit = retrofitBuilder?.baseUrl(COMMON_BASE_URL)?.build()

        return retrofit?.create(SecuritiesApi::class.java)
    }

    fun getCarApi(): CarApi? {
        if (retrofit == null) retrofit = retrofitBuilder?.baseUrl(COMMON_BASE_URL)?.build()

        return retrofit?.create(CarApi::class.java)
    }

    fun getBankApi(): BankApi? {
        if (retrofit == null) retrofit = retrofitBuilder?.baseUrl(COMMON_BASE_URL)?.build()

        return retrofit?.create(BankApi::class.java)
    }
}